package com.shop.cereshop.admin.service.activity.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.admin.dao.activity.CereActivitySignDAO;
import com.shop.cereshop.admin.page.finance.BondCount;
import com.shop.cereshop.admin.page.finance.ShopBond;
import com.shop.cereshop.admin.page.shop.Shop;
import com.shop.cereshop.admin.param.finance.BondParam;
import com.shop.cereshop.admin.service.activity.CereActivitySignService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.activity.CereActivitySign;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CereActivitySignServiceImpl implements CereActivitySignService {

    @Autowired
    private CereActivitySignDAO cereActivitySignDAO;

    @Override
    public void update(CereActivitySign cereActivitySign) throws CoBusinessException {
        cereActivitySignDAO.updateByPrimaryKeySelective(cereActivitySign);
    }

    @Override
    public CereActivitySign findBySignId(Long signId) {
        return cereActivitySignDAO.findBySignId(signId);
    }

    @Override
    public BondCount getAllBond(BondParam param) throws CoBusinessException {
        BondCount count=new BondCount();
        //查询保证金总额
        count.setTotal(cereActivitySignDAO.findBondTotal());
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<ShopBond> list=cereActivitySignDAO.getAllBond(param);
        if(!EmptyUtils.isEmpty(list)){
            list.forEach(shopBond -> {
                //拼接BZJ-报名id查询交易流水号
                shopBond.setTransactionId(cereActivitySignDAO.findTransactionId("BZJ-"+shopBond.getSignId()+"-"));
                //根据保证金状态设置显示缴纳时间/退保时间
                if(IntegerEnum.BOND_REFUND.getCode().equals(shopBond.getBondState())){
                    shopBond.setTime(shopBond.getReturnTime());
                }else {
                    shopBond.setTime(shopBond.getPaymentTime());
                }
            });
        }
        PageInfo<ShopBond> pageInfo=new PageInfo<>(list);
        Page page=new Page(pageInfo.getList(),pageInfo.getTotal());
        count.setPage(page);
        return count;
    }
}
